package com.example.assetmanagement.service.impl;

import com.example.assetmanagement.common.PageRequest;
import com.example.assetmanagement.common.PageResponse;
import com.example.assetmanagement.domain.Role;
import com.example.assetmanagement.mapper.RoleMapper;
import com.example.assetmanagement.service.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class RoleServiceImpl implements RoleService {

    private final RoleMapper roleMapper;

    @Override
    public PageResponse<Role> list(PageRequest pageRequest, String name, Integer status) {
        List<Role> list = roleMapper.selectPage(pageRequest.getOffset(), pageRequest.getSize(), name, status);
        long total = roleMapper.count(name, status);
        return new PageResponse<>(total, list);
    }

    @Override
    public List<Role> getAll() {
        return roleMapper.selectAll();
    }

    @Override
    public Role getById(Long id) {
        return roleMapper.selectById(id);
    }

    @Override
    @Transactional
    public void add(Role role) {
        roleMapper.insert(role);
    }

    @Override
    @Transactional
    public void update(Long id, Role role) {
        role.setId(id);
        roleMapper.update(role);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        roleMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        if (ids == null || ids.isEmpty()) return;
        roleMapper.deleteBatch(ids);
    }

    @Override
    @Transactional
    public void updateStatus(Long id, Integer status) {
        roleMapper.updateStatus(id, status);
    }
} 